//
//  Referrer.m
//  MMLOOK
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "Referrer.h"
#import "AsynConnect.h"

#import "ReferCostumCollectionViewCell.h"
#import "ReferDetailViewController.h"

#import "MJRefreshNormalHeader.h"
#import "MJRefreshAutoNormalFooter.h"
#import "ReferModel.h"
#import "MBProgressHUD+MJ.h"
#import "JudgeConnection.h"
#import "Alert.h"
#define OffWidth [UIScreen mainScreen].bounds.size.width / 375
#define OffHeight [UIScreen mainScreen].bounds.size.height / 667
@interface Referrer ()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic, retain)NSMutableArray *referArray;

@property (nonatomic, retain)UICollectionView *collection;

@property (nonatomic, retain)NSMutableDictionary *Detaildic;
//进行上拉加在后加载的页码数
@property (nonatomic, assign)int i;

@property (nonatomic, retain) MBProgressHUD *mbProgress;


@end

@implementation Referrer

- (void)dealloc
{
    [_collection release];
    [_Detaildic release];
    [_referArray release];
    [_mbProgress release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.referArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    
    
    //
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:253 / 255.0 green:180/255.0 blue:83/255.0 alpha:1];
    self.navigationItem.title = @"爱推荐";
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.i = 2;
    [self creatCollectView];
    [self creatXLSX];
    [self Connect];
    [self creatSLJZ];


    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)creatCollectView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake(160 * OffWidth, 200 * OffHeight);
    
    layout.sectionInset = UIEdgeInsetsMake(15 * OffHeight, 15 * OffWidth, 15 * OffHeight, 15 * OffWidth);
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout] autorelease];
    _collection.backgroundColor = [UIColor whiteColor];
    
    _collection.dataSource = self;
    _collection.delegate = self;
    
    //注册重用池
    [_collection registerClass:[ReferCostumCollectionViewCell class] forCellWithReuseIdentifier:@"重用池"];
    
    [self.view addSubview:_collection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_referArray.count == 0) {
        return 0;
    }
    return _referArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReferCostumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"重用池" forIndexPath:indexPath];
    
    CGFloat red = arc4random() % 256;
    CGFloat green = arc4random() % 256;
    CGFloat blue = arc4random() % 256;
    
    cell.backgroundColor = [UIColor colorWithRed:red / 255.0f green:green / 255.0f blue:blue / 255.0f alpha:1];
    
    ReferModel *model = [_referArray objectAtIndex:indexPath.row];
    [cell sendmodel:model];
    
    return cell;
}


- (void)Connect
{
    [self createBMprogress];
    [AsynConnect asynConnectWithUrlstr:@"http://www.hunliji.com/v1/api/app/newsubjects.json?per_page=20&page=1" parmater:nil block:^(NSData *data) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *array = [dic objectForKey:@"subjects"];
        
        for (NSMutableDictionary *littleDic in array) {
            ReferModel *model = [[[ReferModel alloc] init] autorelease];
            [model setValuesForKeysWithDictionary:littleDic];
            [_referArray addObject:model];
        }
        
        [_collection reloadData];
        [_mbProgress hide:YES];
    }];
}


#warning 菊花效果
-(void)createBMprogress
{
    
    self.mbProgress = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbProgress.mode = MBProgressHUDModeIndeterminate;
    self.mbProgress.labelText = @"加载中...";
    [self.mbProgress show:YES];
    [self.view addSubview:self.mbProgress];
    [self.mbProgress hide: YES];
    [_mbProgress release];

}



//下拉刷新
-(void)creatXLSX
{
    [self.collection.header beginRefreshing];
    self.collection.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [AsynConnect asynConnectWithUrlstr:@"http://www.hunliji.com/v1/api/app/newsubjects.json?per_page=20&page=1" parmater:nil block:^(NSData *data) {
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSMutableArray *array = [dic objectForKey:@"subjects"];
            
            for (NSMutableDictionary *littleDic in array) {
                ReferModel *model = [[[ReferModel alloc] init] autorelease];
                [model setValuesForKeysWithDictionary:littleDic];
                [_referArray addObject:model];
            }
            
            [_collection reloadData];
            [self.collection.header endRefreshing];
        }];

    }];
}




//上拉加载
-(void)creatSLJZ
{
    [self.collection.footer beginRefreshing];
    self.collection.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [AsynConnect asynConnectWithUrlstr:[NSString stringWithFormat:@"http://www.hunliji.com/v1/api/app/newsubjects.json?per_page=20&page=%d",self.i] parmater:nil block:^(NSData *data) {
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSMutableArray *array = [dic objectForKey:@"subjects"];
            
            for (NSMutableDictionary *littleDic in array) {
                ReferModel *model = [[[ReferModel alloc] init] autorelease];
                [model setValuesForKeysWithDictionary:littleDic];
                [_referArray addObject:model];
            }
            [_collection reloadData];
            [self.collection.header endRefreshing];
            self.i++;
        }];
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([JudgeConnection judgeConnection]) {
       
        ReferDetailViewController *referDetail = [[ReferDetailViewController alloc] init];
        referDetail.model = [_referArray objectAtIndex:indexPath.item];
        [self.navigationController pushViewController:referDetail animated:YES];
    }else{
        Alert *alert = [[Alert alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 250) / 2, self.view.frame.size.height * 3 / 4, 250, 50) LabelText:@"当前无网络连接,请检查网络"];
        [self.view addSubview:alert];
        [alert release];
    }
   
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
